<?php
namespace Backend\Controller;
use Think\Controller;
/** 文件上传服务类 **/
class UploadController extends BaseController
{
    
	/** 单张图片上传 **/
	public function imageOne() {
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		$this->saveName    =    array('uniqid','');
		$this->autoSub     =    true;
		$this->subName     =    array('date','Ymd');

		// 上传文件
		$info   = $upload->uploadOne($_FILES['file']);
        $water  = $_GET['isWatermark'] ;
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		} else {

            if($water ==1) {
                $this->water($info);
            }

            $this->ajaxReturn([
				'status'	=> 	'success',
				'msg'		=>	'上传成功',
				'url'	 	=>	'/Public/upload/' . $info['savepath'].$info['savename']
			]);
		}
    }

    private function water($info) {
        //制作水印图片
        $original_picture   = './Public/upload/'.$info['savepath'].$info['savename'];
        $picture_route      = './Public/upload/'.$info['savepath'];
        $watermark_pictures = './Public/water/ad.png';

        $image = new \Think\Image();
        $image->open($original_picture)->water($watermark_pictures,\Think\Image::IMAGE_WATER_NORTHWEST)->save( $picture_route .$info['savename']);

        unlink('./Public/Uploads/'. $info['savepath'].$info['savename']);
    }
}